package com.yanqu.road.server.cmd.activity.summergames.fromcross;

import com.yanqu.road.dao.impl.activity.summergames.SummerGamesUserDataDaoImpl;
import com.yanqu.road.dao.impl.player.UserBagDaoImpl;
import com.yanqu.road.entity.activity.summergames.SummerGamesConfig;
import com.yanqu.road.entity.activity.summergames.SummerGamesExchangeTip;
import com.yanqu.road.entity.activity.summergames.SummerGamesUserData;
import com.yanqu.road.entity.enums.eLogMoneyType;
import com.yanqu.road.entity.log.summergames.LogSummerGamesExchange;
import com.yanqu.road.entity.player.UserBagItem;
import com.yanqu.road.entity.player.UserInfo;
import com.yanqu.road.message.YanQuMessage;
import com.yanqu.road.pb.activity.SummerGamesProto;
import com.yanqu.road.server.command.Cmd;
import com.yanqu.road.server.command.Command;
import com.yanqu.road.server.gameplayer.GamePlayer;
import com.yanqu.road.server.gameplayer.module.activity.summergames.SummerGamesModule;
import com.yanqu.road.server.manger.activity.summergames.SummerGamesActivityMgr;
import com.yanqu.road.server.manger.log.AutoLogMgr;
import com.yanqu.road.server.manger.log.LogMgr;
import com.yanqu.road.server.manger.player.GamePlayerMgr;
import com.yanqu.road.server.manger.player.UserMgr;
import com.yanqu.road.server.protocol.Protocol;
import io.netty.channel.ChannelHandlerContext;

import java.util.Map;

@Cmd(code = Protocol.S_SUMMER_GAMES_EXCHANGE_REFUSE_NOTIFY_FROM_CROSS, desc = "拒绝通知申请者(跨服返回)")
public class SummerGamesRefuseNotifyFromCrossCmd implements Command {

    @Override
    public void execute(ChannelHandlerContext ctx, YanQuMessage yanQuMessage) throws Exception {
        SummerGamesProto.CrossSummerGamesRefuseNotifyMsg backMsg = SummerGamesProto.CrossSummerGamesRefuseNotifyMsg.parseFrom(yanQuMessage.getBytes());
        long playerId = yanQuMessage.getPlayerId();

//        String language = "";
//        UserInfo userInfo = UserMgr.getUserInfo(playerId);
//        if(userInfo != null){
//            language = userInfo.getLanguage();
//        }
//        String mailTitle = ServerLanguageMgr.getContent(MailManager.SUMMER_GAMES_MAIL_TITLE_5, language);
//        String mailContent = ServerLanguageMgr.getContent(MailManager.SUMMER_GAMES_CONTENT_5, language);
//
//        MailManager.sendMail(playerId, 0, eMailType.SYSTEM.getValue(), backItemId + "=1", mailContent, mailTitle, new Date());

        GamePlayer onlinePlayer = GamePlayerMgr.getOnlinePlayer(playerId);
        if(onlinePlayer == null){
            Map<Integer, UserBagItem> userBagItemMap = new UserBagDaoImpl().getUserBagItemMap(playerId);
            for (SummerGamesProto.SummerGamesExchangeInfoTemp temp : backMsg.getInfoList()) {
                UserBagItem userBagItem = userBagItemMap.get(temp.getProvideId());
                if(userBagItem == null){
                    userBagItem = new UserBagItem();
                    userBagItem.setUserId(playerId);
                    userBagItem.setGoodsId(temp.getProvideId());
                    userBagItem.setInsertOption();
                }
                userBagItem.setNum(userBagItem.getNum() + 1);
                userBagItem.setModifyTime(System.currentTimeMillis() / 1000);

                long vipExp = 0;
                UserInfo userInfo = UserMgr.getUserInfoInRAM(playerId);
                if(userInfo != null){
                    vipExp = userInfo.getVipExp();
                }
                LogMgr.addLogGoods(userBagItem.getGoodsId(), playerId, true, 1, userBagItem.getNum(), eLogMoneyType.SummerGamesActivityExchangeRefuseBack, vipExp);

                if(userBagItem.isInsertOption()){
                    new UserBagDaoImpl().add(userBagItem);
                }else {
                    new UserBagDaoImpl().update(userBagItem);
                }
            }
            SummerGamesConfig config = SummerGamesActivityMgr.getSummerGamesConfig();
            if(config != null) {
                SummerGamesUserData userData = new SummerGamesUserDataDaoImpl().getSummerGamesUserData(config.getActivityId(), playerId);
                if(userData != null){
                    for (SummerGamesProto.SummerGamesExchangeInfoTemp temp : backMsg.getInfoList()) {
                        SummerGamesExchangeTip tip = new SummerGamesExchangeTip(temp.getId(), temp.getProvideId(), 2);
                        userData.getExchangeTipMap().put(tip.getId(), tip);
                    }
                    new SummerGamesUserDataDaoImpl().update(userData);
                }
            }
        }else {
            SummerGamesModule gamesModule = onlinePlayer.getModule(SummerGamesModule.class);
            gamesModule.receiveFail(backMsg.getInfoList());
        }

        //被拒绝日志
        SummerGamesConfig summerGamesConfig = SummerGamesActivityMgr.getSummerGamesConfig();
        if(summerGamesConfig != null) {
            for (SummerGamesProto.SummerGamesExchangeInfoTemp info : backMsg.getInfoList()) {
                AutoLogMgr.add(new LogSummerGamesExchange(
                        summerGamesConfig.getActivityId(),
                        info.getPlayerId(),
                        info.getId(),
                        5, //被拒绝
                        info.getAppointUserId(),  //对方
                        info.getProvideId(),   //我方提供
                        info.getNeedId(),    //对方提供
                        info.getType()
                ));
            }
        }
    }
}
